package com.xukai.springboot.controller.statistics;

import com.xukai.springboot.entity.request.statis.AddPlayItemsRequest;
import com.xukai.springboot.entity.request.statis.DelRequest;
import com.xukai.springboot.entity.response.PageResult;
import com.xukai.springboot.entity.response.ResponseBase;
import com.xukai.springboot.entity.response.statis.PlayInfoResponse;
import com.xukai.springboot.entity.statistics.t_statis_play_item;
import com.xukai.springboot.entity.statistics.t_statis_play_main;
import com.xukai.springboot.service.statistics.StatisPlayService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(basePath = "api/statis/play", description = "播放列表管理")
@RestController
@RequestMapping("api/statis/play")
public class StatisPlayController {
    /**
     * 日志
     */
    private final Logger logger = LoggerFactory.getLogger(StatisPlayController.class);

    @Autowired
    StatisPlayService statisPlayService;

    /**
     * 获取播放列表
     */
    @ApiOperation(value = "获取播放列表", notes = "获取播放列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "播放列表名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "播放列表状态 -1：检索全部状态的播放列表", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "start", value = "数据检索起始", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "获取数据数据量，-1：获取全部数据", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetPlayList")
    public ResponseBase<PageResult<t_statis_play_main>> GetPlayList(@RequestParam String name, @RequestParam int state, @RequestParam int start, @RequestParam int pageSize) {
        ResponseBase<PageResult<t_statis_play_main>> result = new ResponseBase<>();
        try {
            PageResult<t_statis_play_main> resultData = new PageResult<>();
            List<t_statis_play_main> data = statisPlayService.GetPlayList(name, state, start, pageSize);
            int total = statisPlayService.GetPlayCount(name, state);
            resultData.setResultData(data);
            resultData.setTotal(total);
            result.setData(resultData);
        } catch (Exception ex) {
            logger.error("获取播放列表", ex);
            result.setStatue(0);
        }
        return result;
    }


    /**
     * 获取播放列表基本信息
     */
    @ApiOperation(value = "获取播放列表基本信息", notes = "获取播放列表基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "播放列表ID", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetPlayBase")
    public ResponseBase<t_statis_play_main> GetPlayBase(@RequestParam int id) {
        ResponseBase<t_statis_play_main> result = new ResponseBase<>();
        try {
            t_statis_play_main data = statisPlayService.GetPlay(id);
            result.setData(data);
        } catch (Exception ex) {
            logger.error("获取播放列表基本信息", ex);
            result.setStatue(0);
        }
        return result;
    }


    /**
     * 新建播放列表
     */
    @ApiOperation(value = "新建播放列表", notes = "新建播放列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "播放列表基础数据", required = true, dataType = "t_statis_play_main", paramType = "body")
    })
    @PostMapping("AddPlay")
    public ResponseBase<t_statis_play_main> AddPlay(@RequestBody t_statis_play_main data) {
        ResponseBase<t_statis_play_main> result = new ResponseBase<>();
        try {
            statisPlayService.AddPlay(data);
            result.setData(data);
        } catch (Exception ex) {
            logger.error("新建播放列表", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 修改播放列表基本信息
     */
    @ApiOperation(value = "修改播放列表基本信息", notes = "修改播放列表基本信息 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "播放列表基础数据", required = true, dataType = "t_statis_play_main", paramType = "body")
    })
    @PostMapping("UpdatePlay")
    public ResponseBase UpdatePlay(@RequestBody t_statis_play_main data) {
        ResponseBase result = new ResponseBase();
        try {
            statisPlayService.UpdatePlay(data);
        } catch (Exception ex) {
            logger.error("新建播放列表", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 删除播放列表基本信息
     */
    @ApiOperation(value = "删除播放列表基本信息", notes = "删除播放列表基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "播放列表删除数据", required = true, dataType = "DelRequest", paramType = "body")
    })
    @PostMapping("DelPlay")
    public ResponseBase DelPlay(@RequestBody DelRequest param) {
        ResponseBase result = new ResponseBase();
        try {
            statisPlayService.DelPlay(param.getId());
        } catch (Exception ex) {
            logger.error("新建播放列表", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 获取场景列表
     */
    @ApiOperation(value = "获取场景列表", notes = "获取场景列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mainID", value = "主表ID", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "start", value = "数据检索起始", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "获取数据数据量，-1：获取全部数据", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetPlayItemList")
    public ResponseBase<PageResult<t_statis_play_item>> GetPlayItemList(@RequestParam int mainID, @RequestParam int start, @RequestParam int pageSize) {
        ResponseBase<PageResult<t_statis_play_item>> result = new ResponseBase<>();
        try {
            PageResult<t_statis_play_item> resultData = new PageResult<>();
            List<t_statis_play_item> data = statisPlayService.GetPlayItemList(mainID, start, pageSize);
            int total = statisPlayService.GetPlayItemCount(mainID);
            resultData.setTotal(total);
            resultData.setResultData(data);
            result.setData(resultData);
        } catch (Exception ex) {
            logger.error("获取场景列表", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 添加场景
     */
    @ApiOperation(value = "添加场景", notes = "添加场景")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "场景数据", required = true, dataType = "t_statis_play_main", paramType = "body")
    })
    @PostMapping("AddPlayItem")
    public ResponseBase<t_statis_play_item> AddPlayItem(@RequestBody t_statis_play_item data) {
        ResponseBase<t_statis_play_item> result = new ResponseBase<>();
        try {
            statisPlayService.AddPlayItem(data);
            result.setData(data);
        } catch (Exception ex) {
            logger.error("添加场景", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 批量添加场景
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "批量添加场景", notes = "批量添加场景")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "场景数据", required = true, dataType = "AddPlayItemsRequest", paramType = "body")
    })
    @PostMapping("AddPlayItems")
    public ResponseBase AddPlayItems(@RequestBody AddPlayItemsRequest param) {
        ResponseBase result = new ResponseBase();
        try {
            if (param == null) {
                return result;
            }
            for (t_statis_play_item data : param.getData()) {
                statisPlayService.AddPlayItem(data);
            }

        } catch (Exception ex) {
            logger.error("批量添加场景", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 修改场景排序
     */
    @ApiOperation(value = "修改场景排序", notes = "修改场景排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "修改场景排序数据", required = true, dataType = "t_statis_play_item", paramType = "body")
    })
    @PostMapping("UpdateItemSort")
    public ResponseBase UpdateItemSort(@RequestBody t_statis_play_item data) {
        ResponseBase result = new ResponseBase();
        try {
            statisPlayService.UpdateItemSort(data.getId(), data.getSort());
        } catch (Exception e) {
            logger.error("修改场景排序", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 删除场景
     */
    @ApiOperation(value = "删除场景", notes = "删除场景")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "删除数据", required = true, dataType = "DelRequest", paramType = "body")
    })
    @PostMapping("DelPlayItem")
    public ResponseBase DelPlayItem(@RequestBody DelRequest param) {
        ResponseBase result = new ResponseBase();
        try {
            statisPlayService.DelPlayItem(param.getId());
        } catch (Exception ex) {
            logger.error("删除场景", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 判断是否存在
     *
     * @param id
     * @param name
     * @return
     */
    @ApiOperation(value = "判断是否存在", notes = "判断是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改数据主键,新建：0，编辑：主键", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "播放列表名称", required = true, dataType = "string", paramType = "query")
    })
    @GetMapping("IsExist")
    public ResponseBase<Boolean> IsExist(@RequestParam int id, @RequestParam String name) {
        ResponseBase<Boolean> result = new ResponseBase<>();
        try {
            result.setData(statisPlayService.IsExit(id, name));
        } catch (Exception e) {
            logger.error("删除场景", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 获取播放列表详细信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取播放列表详细信息", notes = "获取播放列表详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "播放列表主键", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
    })
    @GetMapping("GetPlayInfo")
    public ResponseBase<PlayInfoResponse> GetPlayInfo(@RequestParam int id) {
        ResponseBase<PlayInfoResponse> result = new ResponseBase<>();
        try {
            PlayInfoResponse data = new PlayInfoResponse();
            data.setBase(statisPlayService.GetPlay(id));
            data.setSceneList(statisPlayService.GetPlayItemList(id, 0, -1));
            result.setData(data);
        } catch (Exception e) {
            logger.error("获取播放列表详细信息", e);
            result.setStatue(0);
        }
        return result;
    }

}
